<?php
 if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class User extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->data['controller'] = 'user';         
        $this->load->model('Muser');
        $data = array('loadaudio' => 0);
        $this->session->set_userdata($data);
    }
    
    public function login()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'Tên đăng nhập', 'required');
        $this->form_validation->set_rules('password', 'Mật khẩu', 'required');
        
        if($this->form_validation->run() === TRUE)
        {
            $uname = $this->getParamString('username');
            $upass = $this->getParamString('password');
            $this->data['login'] = $this->Muser->loginUser($uname, $upass);
            if($this->data['login'] == 200)
            {
                redirect('account');
            }else{
                $this->data['result'] = '<h2 style="color: #444;font-weight: bold;">Thông báo lỗi </h2>Sai tên tài khoản hoặc mật khẩu';
            }  
        }
        $this->data['action'] = 'login';
        $this->load->view('template/layout', $this->data);
        
    }
    
    public function logout()
    {
        $this->session->sess_destroy();
        redirect('');
    }
    
    
    public function reconnectgame()
    {
        $this->checkUser($this->session->userdata('user_id'));
        die(json_encode($this->Muser->checkReConnect($this->session->userdata('user_id'))));
    }
    
    public function saveplaycurrent()
    {
        $this->checkUser($this->session->userdata('user_id'));
        die($this->Muser->saveplaycurrentMd());
    }
    
    public function register()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'Tên đăng nhập', 'required|is_unique[user.username]');
        $this->form_validation->set_rules('password', 'Mật khẩu', 'required');
        /*$this->form_validation->set_rules('password', 'Mật khẩu', 'required|matches[vpassword]');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[user.email]');
        $this->form_validation->set_rules('phone', 'Số điện thoại', 'required|is_numeric|min_length[10]');
        */
        if($this->form_validation->run() === TRUE)
        {
            $username = $this->getParamString('username');
            $password = $this->getParamString('password');
            
            $this->data['register'] = $this->Muser->registerUser($username,$password);
            if($this->data['register'] = 200)
            {
                $this->data['result'] = 'Đăng ký thành công';
                $this->data['dkm'] = (object)array('url' => $this->data['baseurl'].'login', 'label' => 'Đăng nhập');
            }   
        }
        
        $this->data['action'] = 'register';
        $this->load->view('template/layout', $this->data);
        
    }
    public function errorpay()
    {
        $this->data['action'] = 'errorpay';
        $this->load->view('template/layout', $this->data);
    }
    public function account()
    {
        $this->checkUser($this->session->userdata('user_id'));
        $this->load->library('form_validation');
        $this->form_validation->set_rules('fullname', 'Họ và tên', 'required');
        $this->form_validation->set_rules('phone', 'Số điện thoại', 'required');
        $this->form_validation->set_rules('address', 'Địa chỉ', 'required');
        $this->form_validation->set_rules('email', 'Địa chỉ Email', 'required');
        
        if($this->form_validation->run() === TRUE)
        {
            $run = $this->Muser->saveInfo($this->session->userdata('user_id')); 
            if($run == 200)
            {
                redirect('account');
            } 
        }
        
        $this->data['data'] = $this->Muser->getDataUser($this->session->userdata('user_id'));  
        $this->data['action'] = 'account';
        $this->load->view('template/layout', $this->data);
    }
    
    public function changepass()
    {
        $this->checkUser($this->session->userdata('user_id'));
        $this->load->library('form_validation');
        $this->form_validation->set_rules('passold', 'Mật khẩu cũ', 'required');
        $this->form_validation->set_rules('passnew', 'Mật khẩu mới', 'required|matches[vpassword]');
        
        if($this->form_validation->run() === TRUE)
        {
            $run = $this->Muser->changePassWd($this->session->userdata('user_id')); 
            if($run == 200)
            {
                redirect('account');
            } 
        }
        
        $this->data['action'] = 'changepass';
        $this->load->view('template/layout', $this->data);
    }
    
    public function trade()
    {
        $this->checkUser($this->session->userdata('user_id'));
        $this->load->library('pagination');
        $config['per_page'] = $this->data['_per_page'] = 10;
        $this->data['page'] = $this->data['page_current'] = $this->getParamUri(2,3);
        $offset = (($this->getParamUri(2,3) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(2,3) - 1) * $config['per_page']) : 0;
        $data = $this->Muser->getTrade($config['per_page'],$offset);
        $config['base_url'] = base_url($this->uri->segment(1).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 2;
        $config['uri_segment_page'] = $this->getParamUri(2,3);
        $config['suffix'] = '.html';
        $config['use_page_numbers'] = TRUE;
        $this->pagination->initialize($config);
        $this->data['data'] = $data['data'];
        
        $this->data['action'] = 'trade';
        $this->load->view('template/layout', $this->data);
    }
    
    public function loginfb()
    {
        require_once PROJECT_PATH."/lib/fb/facebook.php"; //include facebook SDK
        $facebook = new Facebook(array('appId'  => '1441043702790699','secret' => '48a0e1cc2ba7e17478614e72ad1f0b38',));
        
        $user = $facebook->getUser();
        if ($user) {
          try {
            $user = $facebook->api('/me');
          } catch (FacebookApiException $e) {
            error_log($e);
            $user = null;
          }
        }
        
        if ($user) {
          $logoutUrl = $facebook->getLogoutUrl();
        } else {
          $loginUrl = $facebook->getLoginUrl();
        }
        @$vf = $this->getParamString('error');
        @$code = $this->getParamString('code');
        if(in_array(@$logoutUrl, array(null, '', '0')))
        {
            
            if(!in_array($vf, array(null, '', '0')))
            {
                die('<script>window.location="'.$loginUrl.'";</script>');
            }
            if(!in_array($code, array(null, '', '0')))
            {
                if(in_array(@$user['email'], array(null, '', '0')))
                {
                    $email = null;
                }else{
                    $email = @$user['email'];
                }
            
                $name_full = @$user['name'] ? @$user['name'] : $user['id'];
                $name_user = @$user['username'] ? @$user['username'] : $user['id'];
                
                $run = $this->Muser->regDB($name_user, $passwd_new, $email);
                if($run->status == '200')
                {
                    $login = $this->Muser->loginUser($name_user, $passwd_new);   
                    if($login == '200')
                    {
                        die(json_encode((object)array('status' => $login)));
                    }    
                }
            }
            $arr = (object)array('status' => '203', 'url' => $loginUrl);   
            die(json_encode($arr));
        }else{
            $passwd_new = '00000999';
            if(in_array(@$user['email'], array(null, '', '0')))
            {
                $email = null;
            }else{
                $email = @$user['email'];
            }
            $name_full = @$user['name'] ? @$user['name'] : $user['id'];
            $name_user = @$user['username'] ? @$user['username'] : $user['id'];
                    
            $run = $this->Muser->regDB($name_user, $passwd_new, $email);
                
            
            if($run->status == '200')
            {
                $login = $this->Muser->loginUser($name_user, $passwd_new);//  die($login.'-d'); 
                if($login == '200')
                {//die($run->status.'---'.$code);
                    if(!in_array($code, array(null, '', '0')))
                    {//die($code.'--');
                        die('<script>window.location="'.$this->data['baseurl'].'account";</script>');
                    }
                    die(json_encode((object)array('status' => $login)));
                }    
            }
            die('<script>window.location="'.$this->data['baseurl'].'";</script>');
        }
    }
    
    
}
